package com.podium.web.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.podium.comment.AjaxResult;
import com.podium.comment.annotate.OperateLog;
import com.podium.comment.annotate.PermAuth;
import com.podium.comment.constant.OperateLogType;
import com.podium.web.system.entity.SysDictType;
import com.podium.web.system.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 邵文泽
 * @description 字典管理
 * @date 2025-05-09 15:33:44
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 分页获取字典管理信息
     *
     * @param sysDictType 查询字典管理信息条件
     */
    @PermAuth("system:dict_type:list")
    @GetMapping("page")
    public AjaxResult<IPage<SysDictType>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                                   @RequestParam(defaultValue = "10") int pageSize,
                                                   SysDictType sysDictType
                                                     ) {
        Page<SysDictType> page = new Page<>(pageNum, pageSize);
        return new AjaxResult<IPage<SysDictType>>().success(sysDictTypeService.page(page, sysDictType));
    }

    /**
     * 获取字典管理信息
     *
     * @param sysDictType 查询字典管理信息条件
     */
    @PermAuth("system:dict_type:list")
    @GetMapping("list")
    public AjaxResult<List<SysDictType>> getList(SysDictType sysDictType) {
        return new AjaxResult<List<SysDictType>>().success(sysDictTypeService.list(sysDictType));
    }

    /**
     * 根据ID获取字典管理信息
     */
    @GetMapping("{id}")
    public AjaxResult<SysDictType> getByInfo(@PathVariable Long id) {
        return new AjaxResult<SysDictType>().success(sysDictTypeService.getById(id));
    }

    /**
     * 添加字典管理信息
     *
     * @param sysDictType 字典管理信息
     */
    @OperateLog(title = "系统管理-字典管理", businessType = OperateLogType.ADD)
    @PermAuth("system:dict_type:add")
    @PostMapping("/save")
    public AjaxResult<Void> save(@RequestBody SysDictType sysDictType) {
        boolean row = sysDictTypeService.save(sysDictType);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }

    /**
     * 更新字典管理信息
     *
     * @param sysDictType 字典管理信息
     */
    @OperateLog(title = "系统管理-字典管理", businessType = OperateLogType.EDIT)
    @PermAuth("system:dict_type:edit")
    @PostMapping("/update")
    public AjaxResult<Void> update(@RequestBody SysDictType sysDictType) {
        boolean row = sysDictTypeService.updateById(sysDictType);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }

    /**
     * 删除字典管理信息
     *
     * @param ids 字典管理信息ID集合
     */
    @OperateLog(title = "系统管理-字典管理", businessType = OperateLogType.DELETE)
    @PermAuth("system:dict_type:delete")
    @GetMapping("/remove/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        if (ids == null || ids.length == 0) {
            return new AjaxResult<Void>().error("没有要删除的数据！");
        }
        for (Long id : ids) {
            sysDictTypeService.removeById(id);
        }
        return new AjaxResult<Void>().success();
    }

}